//
//  ModifyProfileVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/13.
//  Copyright © 2016年 阳光. All rights reserved.
//  修改简介界面

#import "ModifyProfileVC.h"

@interface ModifyProfileVC ()<UITextViewDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIView *mainView;

@end

@implementation ModifyProfileVC

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    [self initNavBarBtnItem];
    
    self.title = NSLocalizedString(@"My Introduction", nil);//@"简介";
    
    self.view.backgroundColor=WhiteColor;
    
    [self initScrollView];
    
    UITapGestureRecognizer *tapGestureRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView=NO;
    // 将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];
    
}

-(UIView *)setMainView
{
    UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(0,0,APPW,0)];
    mainView.backgroundColor=WhiteColor;
    
 
    UIView *topLine=[UIView new];
    [mainView addSubview:topLine];
    topLine.backgroundColor=MAIN_BACK_COLOR;
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(APPW,kmargin*3/2));
    }];
     setViewHeight_Add(mainView,kmargin*3/2);
    
    // 简介
    UILabel *profileLabel=[self.view getLabelWithText:NSLocalizedString(@"Introduction", nil) font:Font_Number(18) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];//@"简介"
    [mainView addSubview:profileLabel];
    profileLabel.frame=CGRectMake(kmargin,mainView.frame.size.height+kmargin,100,30);
     setViewHeight_Add(mainView,kmargin*3);
    
    
    UITextView *profileView=[UITextView new];
    [mainView addSubview:profileView];
    profileView.showsVerticalScrollIndicator=YES;
    profileView.text=_profileStr;
    profileView.tag=1000;
    profileView.delegate=self;
    [profileView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*2,APPW/2));
        make.top.mas_equalTo(profileLabel.mas_bottom).offset(kmargin);
        make.left.mas_equalTo(kmargin);
    }];
    profileView.delegate=self;
    profileView.backgroundColor=WhiteColor;
    profileView.font=Font_Number(16);
    profileView.textColor=ColorBDBDBD;
    ViewBorderRadius(profileView,kmargin/5,1,MAIN_BACK_COLOR);
     setViewHeight_Add(mainView,APPW/2);
    
    UILabel *limitProfileLabel=[mainView getLabelWithText:[NSString stringWithFormat:@"%d/%d",MaxTextInputValue-_profileStr.length,MaxTextInputValue] font:Font_Number(14) textColor:ColorBDBDBD textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
    [mainView addSubview:limitProfileLabel];
    limitProfileLabel.tag=777;
    [limitProfileLabel bringSubviewToFront:profileView];
    limitProfileLabel.frame=CGRectMake(APPW-100-kmargin*2,mainView.frame.size.height-10,100,30);
    
    
    
    UIButton *saveBtn=[UIButton new];
    [mainView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin*3);
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*6,40));
        make.top.mas_equalTo(profileView.mas_bottom).offset(kmargin*2);
    }];
    saveBtn.backgroundColor=Main_Color;
    saveBtn.titleLabel.font=Font_Number(20);
    ViewBorderRadius(saveBtn,kmargin/2,1,ClearColor);
    [saveBtn setTitle:NSLocalizedString(@"Save", nil) forState:UIControlStateNormal];//@"保存"
    [saveBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(saveBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    
    setViewHeight_Add(mainView,64+kmargin);
    
    return mainView;
}


-(void)initScrollView
{
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH)];
    _scrollView.backgroundColor=WhiteColor;
    _scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_scrollView];
    
    _mainView=[self setMainView];
    setViewCoord_Y(_mainView,0);
    [_scrollView addSubview:_mainView];
    _scrollView.contentSize=CGSizeMake(APPW,CGRectGetMaxY(self.mainView.frame)+kmargin);
    
}

-(void)keyboardHide:(UITapGestureRecognizer*)tap
{
    UITextView *textView=(UITextView *)[self.view viewWithTag:1000];
    [textView resignFirstResponder];
}


// UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([@"\n" isEqualToString:text]==YES)
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

// UITextViewDelegate
-(void)textViewDidChange:(UITextView *)textView
{
    NSString *countStr=[NSString stringWithFormat:@"%d",MaxTextInputValue-textView.text.length];
    NSInteger countNumber=[countStr integerValue];
    UILabel *countLabel=(UILabel *)[self.view viewWithTag:777];
    if (countNumber>0)
    {
        countLabel.text=[NSString stringWithFormat:@"%@/%d",countStr,MaxTextInputValue];
    }
    
    if (textView.text.length>MaxTextInputValue)
    {
        textView.text=[textView.text substringToIndex:MaxTextInputValue];
        [self.view makeToast:@"输入内容超过最大限制" duration:.1 position:CSToastPositionTop];
        countLabel.text=[NSString stringWithFormat:@"0/%d",MaxTextInputValue];
    }
}

-(void)saveBtnEvent
{
    UITextView *textView=(UITextView *)[self.view viewWithTag:1000];
    
    NSDictionary *dic=@{
                        @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
                        @"cn_descript":textView.text
                        };
    
    [QBRequest ModifyDoctorInfoProfileWithDic:dic Success:^(id res)
     {
        [self.view makeToast:res[@"message"] duration:1.0 position:CSToastPositionCenter];
         
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             
             [self.navigationController popViewControllerAnimated:NO];
         });
         
     } failure:^(NSString *error)
     {
         
     }];
}

-(void)initNavBarBtnItem
{
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem initWithNormalImage:@"back_gray" target:self action:@selector(backItemEvent)];
}

-(void)backItemEvent
{
    [self.navigationController popViewControllerAnimated:NO];
}

@end
